home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / sh-utils.12 / sh-utils / sh-utils-1.12 / src / tty.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-01  |  2.9 KB  |  131 lines

  1. /* tty -- print the path of the terminal connected to standard input
  2.    Copyright (C) 90, 91, 92, 93, 1994 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Displays "not a tty" if stdin is not a terminal.
  19.    Displays nothing if -s option is given.
  20.    Exit status 0 if stdin is a tty, 1 if not, 2 if usage error,
  21.    3 if write error.
  22.  
  23.    Written by David MacKenzie <djm@gnu.ai.mit.edu>.  */
  24.  
  25. #include <config.h>
  26. #include <stdio.h>
  27. #include <getopt.h>
  28. #include <sys/types.h>
  29.  
  30. #include "system.h"
  31. #include "version.h"
  32.  
  33. void error ();
  34.  
  35. static void usage ();
  36.  
  37. /* The name under which this program was run. */
  38. char *program_name;
  39.  
  40. /* If nonzero, return an exit status but produce no output. */
  41. static int silent;
  42.  
  43. /* If non-zero, display usage information and exit.  */
  44. static int show_help;
  45.  
  46. /* If non-zero, print the version on standard output and exit.  */
  47. static int show_version;
  48.  
  49. static struct option const longopts[] =
  50. {
  51.   {"help", no_argument, &show_help, 1},
  52.   {"silent", no_argument, NULL, 's'},
  53.   {"quiet", no_argument, NULL, 's'},
  54.   {"version", no_argument, &show_version, 1},
  55.   {NULL, 0, NULL, 0}
  56. };
  57.  
  58. void
  59. main (argc, argv)
  60.      int argc;
  61.      char **argv;
  62. {
  63.   char *tty;
  64.   int optc;
  65.  
  66.   program_name = argv[0];
  67.   silent = 0;
  68.  
  69.   while ((optc = getopt_long (argc, argv, "s", longopts, (int *) 0)) != EOF)
  70.     {
  71.       switch (optc)
  72.     {
  73.     case 0:
  74.       break;
  75.  
  76.     case 's':
  77.       silent = 1;
  78.       break;
  79.  
  80.     default:
  81.       usage (2);
  82.     }
  83.     }
  84.  
  85.   if (show_version)
  86.     {
  87.       printf ("tty - %s\n", version_string);
  88.       exit (0);
  89.     }
  90.  
  91.   if (show_help)
  92.     usage (0);
  93.  
  94.   if (optind != argc)
  95.     usage (2);
  96.  
  97.   tty = ttyname (0);
  98.   if (!silent)
  99.     {
  100.       if (tty)
  101.     puts (tty);
  102.       else
  103.     puts ("not a tty");
  104.  
  105.       if (ferror (stdout) || fclose (stdout) == EOF)
  106.     error (3, errno, "standard output");
  107.     }
  108.  
  109.   exit (isatty (0) ? 0 : 1);
  110. }
  111.  
  112. static void
  113. usage (status)
  114.      int status;
  115. {
  116.   if (status != 0)
  117.     fprintf (stderr, "Try `%s --help' for more information.\n",
  118.          program_name);
  119.   else
  120.     {
  121.       printf ("Usage: %s [OPTION]...\n", program_name);
  122.       printf ("\
  123. \n\
  124.   -s, --silent, --quiet   print nothing, only return an exit status\n\
  125.       --help              display this help and exit\n\
  126.       --version           output version information and exit\n\
  127. ");
  128.     }
  129.   exit (status);
  130. }
  131.